<?php


class pStructure_Browser extends cPart 
{

	function setElement(SimpleXMLElement $parent, $value, cView $view)
	{
		if ($view->doctype == 'raw' || $view->action == 'edit' || $view->action == 'insert') return; 
		$length = $this->getAttribute("length");
		
		if ($view->mode == 'backend') {
			$paging = $this->getAttribute("adminpaging");
		} else if ($view->mode == 'frontend') {
			$paging = $this->getAttribute("paging");
		}
		$method = $this->getAttribute("method");
		
		$nView = new cView(array("length"=>$length, "paging"=>$paging), $view);

		//	u perexů by se gadget neměl používat
		if ($view->length!="full") return;

		$node = $this->getNodes($nView);

		$parent = $parent->addChild("browser");
		
		if ($view->action=="show") 
		{
			if ($method = "view" || $method == "") 
			{
				$s = array();
				if ($view->mode == "frontend") {
					foreach ($node as $n) 
					{
						$s[]= $n->view($nView);
					}
				}
				if ($view->mode == "backend") {
					foreach ($node as $n) 
					{
						$n->setDefinition($n->getDefinition($view));
						$n->setSXML($n->data, $nView, $parent);
					}
				}
				
				//	stránkování
				if ($view->mode == "frontend") {
					$s[]=$this->paging;
				} else if ($view->mode == "backend") {
					$parent->addChild('paging',''.$this->paging.'');
				}
				unset($this->paging);
				
				if ($view->mode == "frontend") {
					$parent->addChild("browser",implode("",$s));
				}
			}
			
			/*//dodělat
			else if (strToLower($method = "getxml")) 
			{
				foreach ($node as $n) 
				{
					$s[]= $n->getSXML($nView);
				}
				$parent->addChild("browser",implode("",$s));
			}
			*/
			//echo $parent->asXML();	
		}
	}
	
	function getNodes($view) 
	{
		$select = $this->getAttribute("select");
		$where = $this->getAttribute("where");
		$from = $this->getAttribute("from");
		
		if ($from!="this") 
		{
			if (is_numeric($from)) 
			{ 
				$source = $this->cms->tree->getNode('id ='.$from, $view); 
			}
			else if ($from=="page") 
			{ 
				$source = $this->cms->tree->getNode('id ='.$this->cms->view->id, $view); 
			}
			else if ($from=="root") 
			{
				$source = $this->cms->tree->getRoot();
			}
		} else $source = $this->node;

		if ($where=="") $where = '1';
		if ($select == "children" || $select == "subnodes") 
		{
			//	vygenerování stránkování
			$cView = new cView(array("paging"=>null, "page"=>null),$view);
			$count = $source->{"get".$select."Count"}($cView);
			$this->paging = $this->getPaging($count, $view);
			
			//	vrácení nodů 
			if ($this->node->order!='') $order =' ORDER BY '.$this->node->order;
			
			return $source->{'get'.$select.'Where'}($where.$order, $view);
		}
	}
	
	function getPaging($count, $view) 
	{
		if ($view->paging<=0) { return; }
		for ($rest=$count, $i=1; $rest>0; $rest-=$view->paging, $i++) {
			if ($view->page!=$i) {
				$s[]="<a href=\"".$view->getUrl(array("paging"=>null, "page"=>$i))."\">$i</a>";
			} else $s[]="<span class=\"actual\">$i</span>";
		}
		if (is_array($s)) {
			if (($prev = $view->page-1)>0) {
				$head = "<a href=\"".$view->getUrl(array("paging"=>null, "page"=>$prev ))."\">«</a> ";
			}
			if (($next = $view->page)*$view->paging<$count) {
				$tail = " <a href=\"".$view->getUrl(array("paging"=>null, "page"=>$next+1 ))."\">»</a>";
			}
			return htmlspecialchars($head.implode(", ",$s).$tail);
		}
	}
	
}
 
?>